'use strict';

const BaseController = require('./base');
const serviceName = 'post';

class PostController extends BaseController {
  constructor(ctx) {
    super(ctx, serviceName);
  }

  // 重写create方法，自动添加会员ID
  async create() {
    const { ctx } = this;
    const data = ctx.request.body;
    // 获取当前登录会员ID
    const memberId = ctx.state.member.id;
    data.memberId = memberId;
    
    await ctx.service[this.serviceName].create(data);
    ctx.success(data, 200);
  }

  // 重写getPageQuery方法，支持按标题搜索
  getPageQuery(query) {
    const where = {};
    if (query.title) {
      where.title = {
        $like: `%${query.title}%`
      };
    }
    if (query.memberId) {
      where.memberId = query.memberId;
    }
    where.status = 1; // 只查询正常状态的帖子
    return where;
  }
}

module.exports = PostController;
